﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HCM_maps_website.Models;
using HCM_maps_website.Process.BLL;

namespace HCM_maps_website.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Search(string keywords)
        {
            List<PlaceModels> Places = new List<PlaceModels>();

            //Tìm Kiếm địa điểm 
            //
            
            DiaChiBLL BLL = new DiaChiBLL();
            Duong duong;
            
            DiaChi DC;
            IList<DiaChi> temp11;
            duong = BLL.FindStreet(keywords);
            if (duong.Name == null)
            {
                DC = BLL.FindArea(keywords);
                if (DC.Area == null)
                {
                    string[] temp = keywords.Split(' ');
                    string number = temp[0];
                    string street = "";
                    if (temp.Length > 2)
                    {
                        for (int i = 1; i < temp.Length; i++)
                        {
                            if (i != temp.Length - 1)
                                street += (temp[i] + " ");
                            else
                                street += temp[i];
                        }
                        DC = BLL.GetDiaChiByString(number, street);
                        if (DC.Area == null)
                        {
                            DC = BLL.GetDiaChiNearByString(number, street);
                            if (DC.Area == null)
                            {
                                string[] temp1 = keywords.Split(' ');
                                string number1 = temp[0];
                                string street1 = "";
                                for (int i = 1; i < temp.Length; i++)
                                {
                                    if (i != temp.Length - 1)
                                        street1 += temp[i] + "%";
                                    else
                                        street1 += temp[i];
                                }

                                temp11 = BLL.FindNotExact(number, street1);
                                if (temp11.Count == 0)
                                {
                                    return null;
                                }
                            }
                            else
                            {
                                //return DC.X.ToString() + "/" + DC.Y.ToString();
                            }
                        }
                        else
                        {
                            //return DC.X.ToString() + "/" + DC.Y.ToString();
                        }
                    }
                }
                else
                {
                   // return DC.X.ToString() + "/" + DC.Y.ToString();
                }
            }
            else
            {
                //return duong.X.ToString() + "/" + duong.Y.ToString();
            }

          

            return View("Index",Places);
            //return PartialView("_SearchForm");
        }
    }
}
